package assetservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type FundSourceEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFundSourceEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FundSourceEditLogic {
	return &FundSourceEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// FundSourceEdit 更新资金来源表
func (l *FundSourceEditLogic) FundSourceEdit(in *property_service.FundSource) (*property_service.Response, error) {
	// 获取资金来源表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).FundSource
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、UpdatedAt、CreatedAt
	if _, err := do.UpdateColumns(&model.FundSource{
		AssetID:       in.AssetID,       // 资产ID
		SourceAmount:  in.SourceAmount,  //资金金额
		SourceName:    in.SourceName,    // 资金来源名称
		SourceAmount1: in.SourceAmount1, //自有资金
		SourceAmount2: in.SourceAmount2, //财政基本拨款
		SourceAmount3: in.SourceAmount3, //科教费用
		SourceAmount4: in.SourceAmount4, //财政项目拨款
		UsageRoom:     in.UsageRoom,     //使用科室ID
		UsageRoomID:   in.UsageRoomID,   //使用科室名称
		Percentage:    in.Percentage,    //占比
		SourceAmount5: in.SourceAmount5, //其他
		SourceType:    in.SourceType,    // 类型
		UpdatedAtuser: in.UpdatedAtuser, // 更新用户
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
